package com.cn.qgx428.lazydiary.ui.activity.subActivity

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.RecordHelper
import com.cn.qgx428.lazydiary.db.bean.CBill
import com.cn.qgx428.lazydiary.db.dao.BillDao
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.ui.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_bill_modify.*
import org.jetbrains.anko.toast

class BillModifyActivity : BaseActivity() {

    lateinit var bill: CBill

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bill_modify)
        initView()
    }

    private fun initView() {
        // 准备数据
        bill = intent.getSerializableExtra("cbill") as CBill

        billModify_total.text = bill.total
        billModify_price.text = "单价    "+ bill.price
        billModify_no.text = "数量    "+ bill.no
        billModify_remark.text = "备注    "+ bill.remark
        billModify_specific.text = bill.specific
        billModify_specificIcon.setImageResource(RecordHelper.getBillIcon(bill.type!!, bill.specific!!))
        billModify_time.text = getFormTime()

        billModify_deleteBtn.setOnClickListener({
            val dao: BillDao = BillDao()
            dao.delete(bill.id)
            toast("已删除")
            LManager.newInstance().trigger(LAction.DB_UPDATE_BILL)
            finish()
        })
        billModify_titleBar.setBackOnClickListener { finish() }
    }

    fun getFormTime(): String {
        var day = bill.createTime
        var startIndex = day.indexOf("年") + 1
        var endIndex = day.indexOf("日")

        var there =  bill.createTime
        var sIndex = there.indexOf("日")

        return day.substring(startIndex..endIndex) + "   " +
                there.subSequence(sIndex+1..there.length-1)
    }
}
